package top.jolyoulu.codegenweb.service.bll.impl;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.jolyoulu.codegenweb.config.TemplateConfig;
import top.jolyoulu.codegenweb.entity.bo.form.MysqlGenCodeFormBO;
import top.jolyoulu.codegenweb.entity.po.GenCodeTask;
import top.jolyoulu.codegenweb.service.bll.ApiGenCodeService;
import top.jolyoulu.codegenweb.service.dal.GenCodeTaskService;
import top.jolyoulu.corecommon.queue.delayed.DelayedTaskEntity;
import top.jolyoulu.corecommon.queue.delayed.DelayedTaskProducer;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @Author: JolyouLu
 * @Date: 2023/4/9 20:14
 * @Description
 */
@Slf4j
@Service
public class ApiGenCodeServiceImpl implements ApiGenCodeService {

    private TemplateConfig templateConfig;

    @Autowired
    private GenCodeTaskService genCodeTaskService;

    @Autowired
    private DelayedTaskProducer delayedTaskProducer;

    @Override
    @Transactional
    public String gen(MysqlGenCodeFormBO bo) {
        //生成一个任务
        GenCodeTask task = new GenCodeTask();
        task.setId(UUID.randomUUID().toString().replaceAll("-",""));
        task.setProgress(1);
        task.setDataSourceId(bo.getDataSourceId());
        task.setTaskBo(JSONObject.toJSONString(bo));
        if (genCodeTaskService.save(task)){
            delayedTaskProducer.put(new DelayedTaskEntity<>(task.getId(), LocalDateTime.now(),bo));
        }
        return null;
    }
}
